package ferus.tigris.easymarks;

import java.util.Random;

import android.graphics.Point;

public class EasyMarkBehavior implements AbstractBehavior{
	GameView gameView;
	AbstractView view;
	private Point position = new Point(0, 20);
	private Vector2D vector = new Vector2D(10, 0);

	public EasyMarkBehavior(GameView gameView, AbstractView view) {
		this.gameView = gameView;
		this.view = view;

		Random rnd = new Random();
		position = new Point(rnd.nextInt(gameView.getWidth()), rnd.nextInt(gameView.getHeight()));
		vector = new Vector2D(rnd.nextInt(10) - 5, rnd.nextInt(5) - 10);
	}

	@Override
	public void update() {
		position.offset(vector.x, vector.y);
		validateCoordsAndChangeVector();
		view.setPosition(position);
		view.setVector(vector);
	}

	private void validateCoordsAndChangeVector() {
		Random rnd = new Random();
		int xSpeed = rnd.nextInt(5) - 5;
		int ySpeed = rnd.nextInt(5) - 5;

		if(position.x + view.width() > gameView.getWidth()) {
			vector.x = xSpeed;
			vector.y = ySpeed;
		}
		if(position.y + view.height() > gameView.getHeight()) {
			vector.x = xSpeed;
			vector.y = ySpeed;
		}
		if(position.x < 0) {
			vector.x = -xSpeed;
		}
		if(position.y < 0) {
			vector.y = -ySpeed;
		}
	}

	@Override
	public AbstractView view() {
		return view;
	}

	public boolean isCollition(float xTouch, float yTouch) {
		return xTouch > position.x && xTouch < position.x + view.width() && yTouch > position.y && yTouch < position.y + view.height();
	}

}
